﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OracleClient;
using DreamBuilder.Common;
using DreamBuilder.Core.Message;

namespace Web.SystemConfig.DataAccess
{
    public class DALAuth_Menu : DataAccessBase
    {
        internal DataTable CheckMenuParentId(string parentId)
        {
            DbParameter[] arrParam;
            string strSql = string.Empty;
            if (DBHelper.database is Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase)
            {
                arrParam = new DbParameter[] {
                    new OracleParameter(":Parent_ID", parentId) 
                };
                strSql = "SELECT * FROM P_Auth_Menu where Parent_Menu_ID=:Parent_ID ";
            }
            else
            {
                arrParam = new DbParameter[] {
                    new SqlParameter("@Parent_ID", parentId)
                };
                strSql = "SELECT * FROM P_Auth_Menu where Parent_Menu_ID=@Parent_ID ";
            }
            return DBHelper.ExecuteDataSet(strSql, arrParam).Tables[0];

        }

        internal DataTable GetAllMenuData()
        {
            string strSql = "SELECT * FROM P_Auth_Menu ";
            return DBHelper.ExecuteDataSet(strSql).Tables[0];
        }

        internal DataSet GetDeleteChildInfo(string pkValue)
        {
            DataSet dsInfo = new DataSet();
            DataTable dtInfo = null;
            string sSql = "select P_Auth_Function.*,'3' as RowStatus from P_Auth_Function where Menu_ID = '" + pkValue + "'";
            dtInfo = DBHelper.ExecuteDataSet(sSql).Tables[0];
            dtInfo.PrimaryKey = new DataColumn[] { dtInfo.Columns["Func_ID"] };
            dtInfo.TableName = "P_Auth_Function";
            dsInfo.Tables.Add(dtInfo.Copy());
            return dsInfo;
        }

        internal ResultInfo DeleteMenu(string pkValue)
        {
            ResultInfo result = new ResultInfo();
            bool breturn = false;
            string strsql = @"delete P_Auth_Role_Function where Func_ID in (select Func_ID From P_Auth_Function where Menu_ID='" + pkValue + "')";
            int count = DBHelper.ExecuteSql(strsql);
            breturn = count > 0 ? true : false;
            strsql = @"delete P_Auth_Function where Menu_ID='" + pkValue + "'";
             count = DBHelper.ExecuteSql(strsql);
             breturn = count > 0 ? true : false & breturn;
            strsql = @"delete P_Auth_Menu where Menu_ID='" + pkValue + "'";
            count = DBHelper.ExecuteSql(strsql);
            breturn = count > 0 ? true : false & breturn;
            result.bSuccess = breturn;
            return result;
        }

        internal DataTable GetFuncName(string Func_Name_ID)
        {
            string strsql = "select Code_Desc from P_BT_CodeTable where CodeTableID = '" + Func_Name_ID + "'";
            return DBHelper.ExecuteDataSet(strsql).Tables[0];
        }
        #region 保存菜单多语信息 add by lyn 20120503
        /// <summary>
        /// 保存菜单多语信息
        /// </summary>
        /// <param name="menuID">菜单ID</param>
        /// <param name="menuDesc">菜单描述</param>       
        /// <returns></returns>
        internal ResultInfo SaveMenuDescInfo(string menuID, string menuDesc)
        {
            string strsql = "insert into config_res_system values('Public/Modules','" + menuID + "','zh-CN','" + menuDesc + "')";
            int count = DBHelper.ExecuteSql(strsql);
            ResultInfo result = new ResultInfo();
            if (count > 0)
            {
                result.bSuccess = true;
            }
            else
            {
                result.bSuccess = false;
            }
            return result;
        }
        #endregion
        #region 删除菜单多语信息 add by lyn 20120503
        /// <summary>
        /// 删除菜单多语信息
        /// </summary>
        /// <param name="pkValue">菜单ID</param>               
        /// <returns></returns>
        internal ResultInfo DeleteMenuDescInfo(string pkValue)
        {
            string strsql = "delete from config_res_system where vPageUrl='Public/Modules' and vCulture='zh-CN' and vStringKey='" + pkValue + "'";
            int count = DBHelper.ExecuteSql(strsql);
            ResultInfo result = new ResultInfo();
            if (count > 0)
            {
                result.bSuccess = true;
            }
            else
            {
                result.bSuccess = false;
            }
            return result;
        }
        #endregion
        #region
        /// <summary>
        /// 判断菜单在菜单多语表中是否存在
        /// </summary>
        /// <param name="pkValue">菜单id</param>
        /// <returns>数量</returns>
        internal int IsMenuID(string pkValue)
        {
            string strsql = "select COUNT(*) from Config_Res_System where vStringKey='" +pkValue+ "' and vPageUrl='Public/Modules' and vCulture='zh-CN'";
            int obj = Convert.ToInt32(DBHelper.ExecuteScalar(strsql));
            return obj;
        }
        #endregion
        #region 修改菜单多语信息 add by lyn 20120511
        /// <summary>
        /// 修改菜单多语信息
        /// </summary>
        /// <param name="pkValue">菜单ID</param>               
        /// <returns></returns>
        internal ResultInfo UpdateMenuDescInfo(string pkValue,string menuDesc)
        {
            string strsql = "update Config_Res_System set vDesc='"+ menuDesc +"' where vPageUrl='Public/Modules' and vCulture='zh-CN' and vStringKey='" + pkValue + "'";
            int count = DBHelper.ExecuteSql(strsql);
            ResultInfo result = new ResultInfo();
            if (count > 0)
            {
                result.bSuccess = true;
            }
            else
            {
                result.bSuccess = false;
            }
            return result;
        }
        #endregion

        /// <summary>
        /// 获取菜单多语信息
        /// </summary>
        /// <param name="menuid"></param>
        /// <returns></returns>
        internal string GetMenuDesc(string menuid)
        {
            string menuDesc = null ;
            string sql = "select vdesc from Config_Res_System where vStringKey='" + menuid + "' and vPageUrl='Public/Modules' and vCulture='zh-CN'";
            using (IDataReader dr = DBHelper.ExecuteReader(sql))
            {
                if (dr.Read())
                {
                    menuDesc = dr[0].ToString();
                }
            }
            return menuDesc;
        }

        internal string GetMenuIdByUrl(string url)
        {
            string sql = "select * from P_Auth_Menu where menu_url='" + url + "'";
            DataSet ds = this.DBHelper.ExecuteDataSet(sql);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0]["menu_id"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

    }
}
